package domain.service.base;

import core.util.ValidationUtil;
import domain.entity.base.BaiduInfo;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.springframework.stereotype.Service;
import util.Md5Util;
import util.StringUtil;

import java.text.Collator;
import java.util.*;

/**
 * Created by bowen on 2017-08-14.
 */
@Service
public class ValidationService {

//    public Map<String, Object> getSignature(String url) throws Exception {
//        String ticket = ValidationUtil.getTicket();
//        if (!StringUtil.isNullOrEmpty(ticket)) {
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("noncestr", Md5Util.generateShortUuid());
//            map.put("jsapi_ticket", ticket);
//            map.put("timestamp", System.currentTimeMillis() / 1000);
//            map.put("url", url);
//            List<String> listStr = new ArrayList<String>();
//            for (String key : map.keySet()) {
//                listStr.add(key);
//            }
//            Collections.sort(listStr, Collator.getInstance(java.util.Locale.CHINA));
//            StringBuffer stringBuffer = new StringBuffer();
//            for (int i = 0; i < listStr.size(); i++) {
//                Object object = map.get(listStr.get(i));
//                if (i < listStr.size() - 1)
//                    stringBuffer.append(listStr.get(i) + "=" + object + "&");
//                else
//                    stringBuffer.append(listStr.get(i) + "=" + object);
//            }
//            String sign = Md5Util.hash(stringBuffer.toString(), Md5Util.type_sha1);
//            map.put("signature", sign);
//            map.put("appId", ValidationUtil.corpId);
//            return map;
//        } else {
//            return null;
//        }
//    }

    public BaiduInfo getBaiduInfo(float latitude, float longitude) throws Exception {
        String baidu_url = "http://api.map.baidu.com/geoconv/v1/?coords=" + latitude + "," + longitude + "&from=1&to=5&ak=BAI_KEY";
        baidu_url = baidu_url.replace("BAI_KEY", ValidationUtil.baidu_ak);
        ClientRequest request = new ClientRequest(baidu_url);
        request.accept("application/json");
        ClientResponse<String> response = request.get(String.class);
        String responseContent = (String) response.getEntity();
        System.out.println(responseContent);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(responseContent, new TypeReference<BaiduInfo>() {
        });
    }
}
